//
//  TwoViewController.m
//  test
//
//  Created by qianfeng on 15/6/18.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "TwoViewController.h"
#import "ImageTool.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface TwoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initUI];
}

-(void)initUI
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(20, 80, self.view.frame.size.width-40, 40);
    [button setTitle:@"调用系统相册" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor blueColor];
    button.layer.cornerRadius=8;
    button.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    CGRect frame=button.frame;
    frame.origin.y=CGRectGetMaxY(frame)+20;
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame=frame;
    [button1 setTitle:@"调用相机" forState:UIControlStateNormal];
    button1.backgroundColor=[UIColor blueColor];
    button1.layer.cornerRadius=8;
    button1.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}
-(void)buttonClick:(UIButton *)button
{ if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    [self loadImageWith:UIImagePickerControllerSourceTypeCamera];
}
else
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"无法调用" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    [alert show];
    
}
    
}
-(void)btnClick:(UIButton *)button
{
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        [self loadImageWith:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"无法调用" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
        [alert show];
        
    }
}
-(void)loadImageWith:(UIImagePickerControllerSourceType)type
{
    UIImagePickerController *controller=[[UIImagePickerController alloc]init];
    controller.sourceType=type;
    controller.delegate=self;
    controller.allowsEditing=YES;
    [self presentViewController:controller animated:YES completion:nil];
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
    {
        UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
        self.view.backgroundColor=[UIColor colorWithPatternImage:image];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}







-(void)findNotification:(NSNotification *)notification
{
    NSDictionary *ary=notification.userInfo;
    self.view.backgroundColor=ary[@"color"];
}

@end
